package org.goldfish.distributed_framework;

import org.goldfish.distributed_framework.client.*;
import org.goldfish.distributed_framework.config.*;

import org.goldfish.distributed_framework.registry.*;
import org.goldfish.distributed_framework.server.*;

import java.io.IOException;


public class GoldfishFramework {

    public static void runServer() {
        Object config = ConfigLoader.loadConfig();
        try {
            if (config instanceof ServerConfig) {
                RpcServer.start((ServerConfig) config);
            } else {
                throw new IllegalStateException("配置类型不是 ServerConfig，当前配置文件不匹配");
            }
        } catch (Exception e) {
            throw new IllegalStateException("服务端启动失败", e);
        }
    }

    public static void runRegistry() {
        Object config = ConfigLoader.loadConfig();
        try {
            if (config instanceof RegistryConfig) {
                RegistryServer.start((RegistryConfig) config);
            } else {
                throw new IllegalStateException("配置类型不是 RegistryConfig，当前配置文件不匹配");
            }
        } catch (IOException e) {
            throw new IllegalStateException("注册中心启动失败", e);
        }
    }

    public static void initClient() {
        Object config = ConfigLoader.loadConfig();
        if (config instanceof ClientConfig) {
            RpcClient.setup((ClientConfig) config);
        } else {
            throw new IllegalStateException("配置类型不是 ClientConfig，当前配置文件不匹配");
        }
        System.out.println("客户端启动成功");
    }
}


